﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using game_for_test_helper.src.util;

namespace game_for_test_helper.src.helper
{
    public class TextWriteHelper : DrawableGameComponent
    {
        private static List<TextElement> textlist = new List<TextElement>();
        private Texture2D texture_font;
        private SpriteBatch spriteBatch;
        private ContentManager content;
        public ContentManager Content
        {
            get { return content; }
            set { content = value; }
        }

        public TextWriteHelper(Game game)
            : base(game)
        {


        }
        public void Write(Vector2 pos, String text)
        {
            textlist.Add(new TextElement(pos, text));
        }
        public void Write(Vector2 pos, String text, Color color)
        {
            textlist.Add(new TextElement(pos, text, color));
        }
        public static void  StaticWrite(Vector2 pos, String text)
        {
            textlist.Add(new TextElement(pos, text));
        }
        public override void Initialize()
        {
            //System.Console.WriteLine("TextWrite Init");

            base.Initialize();
        }
        public override void Draw(GameTime gameTime)
        {
            
            
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.Immediate,SaveStateMode.SaveState);
            GraphicsDevice.RenderState.DepthBufferEnable = false;
            foreach (TextElement element in textlist)
            {
                int width = 0;
                foreach (char c in element.Message)
                {
                    Rectangle rect=TextureFonts.getRectangle(c);
                    element.Position = new Vector2(element.Position.X+width, element.Position.Y);
                    spriteBatch.Draw(texture_font, element.Position,rect, element.Color);
                    width =rect.Width;
                }
            }
            GraphicsDevice.RenderState.DepthBufferEnable = true;
            spriteBatch.End();
            textlist.Clear();
            base.Draw(gameTime);
            
        }
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }
        protected override void LoadContent()
        {
            //this.device = this.GraphicsDevice;
            spriteBatch = new SpriteBatch(this.GraphicsDevice);
            content = GameStaticHolder.ContentHolder1;
            texture_font = this.content.Load<Texture2D>("Texture/DayDream");
            //System.Console.WriteLine("TextWrite LoadContent");
            base.LoadContent();
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            //GameStaticHolder.GraphicsDevice1.RenderState.DepthBufferEnable = true;
        }

        class TextElement
        {
            private Vector2 position;
            private String message;
            private Color color;
            public Microsoft.Xna.Framework.Graphics.Color Color
            {
                get { return color; }
                set { color = value; }
            }
            public Microsoft.Xna.Framework.Vector2 Position
            {
                get { return position; }
                set { position = value; }
            }
            public System.String Message
            {
                get { return message; }
                set { message = value; }
            }
            public TextElement(Vector2 pos, String message, Color color)
            {
                this.position = pos;
                this.message = message;
                this.color = color;
            }
            public TextElement(Vector2 pos, String message)
            {
                this.position = pos;
                this.message = message;
                this.color = Color.White;
            }
        }
    }
}
